package com.manman.eduService.controller;


import com.manman.commonUtils.Result;
import com.manman.eduService.entity.EduChapter;
import com.manman.eduService.entity.chapter.ChapterVo;
import com.manman.eduService.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    EduChapterService eduChapterService;

    // 课程大纲列表，根据课程 id 进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("items", list);
    }

    // 添加章节
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody EduChapter educhapter){
        eduChapterService.save(educhapter);
        return Result.ok();
    }

    // 根据章节 id 查询
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return Result.ok().data("chapter", eduChapter);
    }

    // 修改章节
    @PostMapping("updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return Result.ok();
    }

    // 删除章节
    @DeleteMapping("{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId){
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if(flag){
            return Result.ok();
        }
        return Result.error();
    }
}
